// pages/book/index/book-index.ts
import {formatNumber} from "../../../utils/util";

Page({

  /**
   * 页面的初始数据
   */
  data: {
    time: "",
    day: "今日"
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad() {
    let eventChannel = this.getOpenerEventChannel();
    eventChannel.on("sendData", item => {
      this.setData({ item });
    })
    // 模拟数据加载
    this.setData({
      plateList: [
        { carNum: "川A6T212", cid: 1 },
        { carNum: "川A30Q17", cid: 2 },
      ]
    })
  },
  onUnload() {
    console.log("预订onUnload")
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {
    let now = new Date(new Date().getTime() + 30 * 1000 * 60);
    let time = formatNumber(now.getHours()) + ":" + formatNumber(now.getMinutes());
    this.setData({time})
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },
  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 时间选择
   */
  bindTimeChange(e:WechatMiniprogram.PickerChange) {
    let time = <string><any>e.detail.value;
    let hour = parseInt(time);  
    let nowHour = new Date().getHours();
  
    let day = "今日";
    if (hour < nowHour) {
      day = "明日";
    }
    this.setData({
      day,
      time
    })
  },

  /**
   * 点击车牌列表
   */
  handleSelect(e: WechatMiniprogram.CustomEvent) {
    let item = e.detail;
    wx.showModal({
      title: "预约确认",
      content: `确认预约${item.carNum}车牌吗`,
      success(res) {
        if (res.confirm) {
          console.log('用户单击确定');
        }
      }
    })
  },
})